package com.foolishbird.sdk.core;


import com.foolishbird.sdk.handler.SimpleAsyncResponseHandler;
import com.foolishbird.sdk.params.BindPhoneResponseParams;
import com.foolishbird.sdk.params.IShowPayIdcardPopup;
import com.foolishbird.sdk.params.LoginResponseParams;
import com.foolishbird.sdk.params.PayChannelResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.PayChannelInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.utils.WL;

public class HttpEngineInterceptor extends SimpleAsyncResponseHandler<ResponseParams> {

    private static final String TAG = HttpEngineInterceptor.class.getSimpleName();

    private final SimpleAsyncResponseHandler mHandler;

    public HttpEngineInterceptor(SimpleAsyncResponseHandler handler) {
        super((handler == null) ? ResponseParams.class : handler.getResponseClass());
        this.mHandler = handler;
    }

    @Override
    public void onAsyncResponseSuccess(int flag, ResponseParams response) {
        int stateCode = response.getState().getCode();
        if (response instanceof LoginResponseParams) {
            if (State.SUCCESS == stateCode) {
                replaceLoginResponseParams(response);
                HttpEngineApi.getInstance().getEngineConduits().isLoggedIn = true;
                HttpEngineApi.getInstance().getEngineConduits().writeAutoLogin(true);
                HttpEngineApi.getInstance().getEngineConduits().getPlatformConfigInfo().resetFlowStack();
//                Object responseData = response.getData();
                if (response instanceof LoginResponseParams) {
                    LoginResponseParams.LoginResponseData loginResponseData = ((LoginResponseParams) response).getData();
                    if (loginResponseData != null) {
                        HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
                        conduits.saveCurrentLoginUserTickit(loginResponseData.getToken());
//                        conduits.realnameInfo.setUserAdultState(loginResponseData.isAdult());
                        conduits.writeNeedIdentify(loginResponseData.isAdult());
                        conduits.writeNeedBindPhone(loginResponseData.getUser().getPhoneStatus()!=1);
//                        conduits.setOpenWindowContentView(loginResponseData.isOpenWindowContent());
                    }
                }
            }
        }  else if (response instanceof PayChannelResponseParams) {
            PayChannelInfo payChannelInfo = ((PayChannelResponseParams) response).getData();
            if (payChannelInfo != null) {
//                HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
//                ConfigInfo configInfo = conduits.getPlatformConfigInfo();
//                if (configInfo != null) {
//                    configInfo.setWxAppId(payChannelInfo.getWxAppId());
//                    configInfo.setWxAppletOriginalId(payChannelInfo.getWxAppletOriginalId());
//                    conduits.setPlatformConfigInfo(configInfo);
//                }
            }
//        } else if (response instanceof PushInfoResponseParams) {
//            PushInfoResponseParams.PushInfoResponseData responseData = ((PushInfoResponseParams) response).getData();
////            if (responseData != null) {
////                HttpEngineApi.getInstance().getEngineConduits().saveLastPushTime(responseData.getFirstSaveTime());
////            }
        }else if (response instanceof BindPhoneResponseParams){
            if (State.SUCCESS == stateCode) {
                BindPhoneResponseParams.BindPhoneResponseData payChannelInfo = ((BindPhoneResponseParams) response).getData();
                if(payChannelInfo.getResult()==1){
                    HttpEngineApi.getInstance().getEngineConduits().writeNeedIdentify(true);
                }else {
                    HttpEngineApi.getInstance().getEngineConduits().writeNeedBindPhone(false);
                }
            }
        }
//        else if(response instanceof ConfigInfoResponseParams){
//            ConfigInfoResponseParams responseParams = (ConfigInfoResponseParams) response;
//            int stateCode = responseParams.getState().getCode();
//            if (State.SUCCESS == stateCode) {
//                ConfigInfo config = responseParams.getData();
////                HttpEngineApi.getInstance().getEngineConduits().saveClosePhoneLogin(config.getColosePhoneLogin());
////                HttpEngineApi.getInstance().getEngineConduits().saveReportActive(config.getIsReport());
////                HttpEngineApi.getInstance().getEngineConduits().saveIDcardUiType(config.getIdcardUIType());
////                HttpEngineApi.getInstance().getEngineConduits().saveFloatWindowType(config.getMiniWord());
//                HttpEngineApi.getInstance().getEngineConduits().setPlatformConfigInfo(config);
//
//            }
//
//        }
        else {
            if (response != null) {
                WL.w(TAG, response.getClass().getSimpleName() + " instance is not intercepted");
            }
        }

        if (mHandler != null) mHandler.onAsyncResponseSuccess(flag, response);
    }


    private void replaceLoginResponseParams(ResponseParams response) {
        WL.i(TAG, "replaceLoginResponseParams ------------>");
//        ConfigInfo configInfo = HttpEngineApi.getInstance().getEngineConduits().getPlatformConfigInfo();
//        if (configInfo != null && configInfo.isNotice()) {
//            if (response instanceof LoginResponseParams) {
//                UserInfo userInfo = ((LoginResponseParams) response).getData().getUser();
//                userInfo.setInfo_popup(configInfo.getNoticePopup());
//            } else if (response instanceof RegisterResponseParams) {
//                ((RegisterResponseParams) response).getData().setInfo_popup(configInfo.getNoticePopup());
//            } else if (response instanceof QuickRegisterResponseParams) {
//                ((QuickRegisterResponseParams) response).getData().setInfo_popup(configInfo.getNoticePopup());
//            } else {
//                WL.i(TAG, response.getClass().getSimpleName() + " instance is not replace params.");
//            }
//        }
    }

    @Override
    public void onAsyncCancel(int flag) {
        super.onAsyncCancel(flag);
        if (mHandler != null) mHandler.onAsyncCancel(flag);
    }

    @Override
    public void onAsyncSendRequest(int flag) {
        super.onAsyncSendRequest(flag);
        if (mHandler != null) mHandler.onAsyncSendRequest(flag);
    }

    @Override
    public void onAsyncException(int flag, Exception ex) {
        super.onAsyncException(flag, ex);
        if (mHandler != null) mHandler.onAsyncException(flag, ex);
    }

    @Override
    public void onAsyncNetworkException(int flag, Exception ex) {
        super.onAsyncNetworkException(flag, ex);
        if (mHandler != null) mHandler.onAsyncNetworkException(flag, ex);
    }
}
